package cn.cas.mango.service;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Data
public class ConfigService {

  @Value("${elasticsearch.index}")
  private String index;

  @Value("${history.path}")
  private String historyPath;
  @Value("${fragment.size}")
  private Integer fragmentSize;
  @Value("${elasticsearch.host}")
  private String host;
  @Value("${elasticsearch.port}")
  private int port;
  @Value("${elasticsearch.username:-}")
  private String username;
  @Value("${elasticsearch.password:-}")
  private String password;
  @Value("${default.hits.count}")
  private int hitsCount;
  @Value("${web.url}")
  private String mediaUrl;
  @Value("${boost}")
  private float boost;



  @Value("${aggTimes}")
  private Double aggTimes;
  @Value("${read.batch.size}")
  private int readBatchSize;

  @Value("${cluster.page.size}")
  private int pageSize;
  @Value("${cluster.like.degree}")
  private Double likeDegree;

  @Value("${max.read.size}")
  private Integer maxReadSize;

  @Value("${cluster.default.platform}")
  private String defaultPlatform;

  @Value("${cluster.default.Chinese.ratio}")
  private Double chineseRatio;


  @Value("${cluster.filter.not.Chinese}")
  private boolean clusterFilterNotChinese;


  @Value("${searchHot.default.if.agg}")
  private Boolean defaultIfAgg;

  @Value("${searchHot.default.pageSize}")
  private Integer searchHotPageSize;


  @Value("${searchHot.default.searchMode}")
  private Integer searchMode;

  @Value("${searchHot.default.simWeight}")
  private Double simWeight;

  @Value("#{'${getAssembleHot.no.vector.subjects}'.split(',')}")
  private List<String> getAssembleHotNoVectorSubjects;

  @Value("${searchHot.default.aggWeight}")
  private Double searchHotAggregationWeight;

  @Value("${cluster.keywords}")
  private boolean keywordsCluster;

  @Value("${model.summary.url}")
  private String modelSummaryUrl;


  @Value("${vector.size}")
  private Integer vectorSize;

  @Value("${elasticsearch.alias.index}")
  private String aliasIndex;
  private final Map<Integer, String> colorMap = new HashMap<Integer, String>(){{
    put(1, "#337AB7");
    put(2, "#54BA9A");
    put(3, "#FF7D3D");
    put(4, "#E55050");
  }};


  public Map<String, String> number2AreaCast = new HashMap<>();
  {
    number2AreaCast.put( "0002领域", "经济领域");
    number2AreaCast.put( "0003领域", "核领域");
    number2AreaCast.put( "0004领域", "政治领域");
    number2AreaCast.put( "0005领域", "军事领域");
    number2AreaCast.put( "0006领域", "科技领域");
    number2AreaCast.put( "0007领域", "国土领域");
    number2AreaCast.put( "0008领域", "社会领域");
    number2AreaCast.put( "0009领域", "文化领域");
    number2AreaCast.put( "0010领域", "网络领域");
    number2AreaCast.put( "0011领域", "生态领域");
    number2AreaCast.put( "0012领域", "资源领域");
    number2AreaCast.put( "0013领域", "金融领域");
    number2AreaCast.put( "0014领域", "太空领域");
    number2AreaCast.put( "0015领域", "极地领域");
    number2AreaCast.put( "0016领域", "深海领域");
    number2AreaCast.put( "0017领域", "生物领域");
    number2AreaCast.put( "0018领域", "海外利益领域");
    number2AreaCast.put( "0019领域", "数据领域");
    number2AreaCast.put( "0020领域", "人工智能领域");
    number2AreaCast.put( "0021领域", "粮食领域");
  }

  public Map<String, String> area2number = new HashMap<>();
  {
    area2number.put( "经济领域", "0002领域");
    area2number.put( "核领域", "0003领域");
    area2number.put( "政治领域", "0004领域");
    area2number.put( "军事领域", "0005领域");
    area2number.put( "科技领域", "0006领域");
    area2number.put( "国土领域", "0007领域");
    area2number.put( "社会领域", "0008领域");
    area2number.put( "文化领域", "0009领域");
    area2number.put( "网络领域", "0010领域");
    area2number.put( "生态领域", "0011领域");
    area2number.put( "资源领域", "0012领域");
    area2number.put( "金融领域", "0013领域");
    area2number.put( "太空领域", "0014领域");
    area2number.put( "极地领域", "0015领域");
    area2number.put( "深海领域", "0016领域");
    area2number.put( "生物领域", "0017领域");
    area2number.put( "海外利益领域", "0018领域");
    area2number.put( "数据领域", "0019领域");
    area2number.put( "人工智能领域", "0020领域");
    area2number.put( "粮食领域", "0021领域");
  }




  private Set<String> allProvince = new HashSet<>();
  {
    allProvince.add("中国");
    allProvince.add("广西");
    allProvince.add("湖北");
    allProvince.add("北京");
    allProvince.add("湖南");
    allProvince.add("天津");
    allProvince.add("广东");
    allProvince.add("河北");
    allProvince.add("海南");
    allProvince.add("山西");
    allProvince.add("重庆");
    allProvince.add("内蒙古");
    allProvince.add("四川");
    allProvince.add("辽宁");
    allProvince.add("贵州");
    allProvince.add("吉林");
    allProvince.add("云南");
    allProvince.add("黑龙江");
    allProvince.add("陕西");
    allProvince.add("上海");
    allProvince.add("甘肃");
    allProvince.add("江苏");
    allProvince.add("青海");
    allProvince.add("浙江");
    allProvince.add("宁夏");
    allProvince.add("福建");
    allProvince.add("新疆");
    allProvince.add("江西");
    allProvince.add("山东");
    allProvince.add("河南");
    allProvince.add("西藏");
    allProvince.add("安徽");
    allProvince.add("台湾");
    allProvince.add("香港");
    allProvince.add("澳门");
  }


  private Set<String> blackList = new HashSet<>();
  {
    try {
      blackList.addAll(getBlackList1("blacklist.txt"));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  public List<String> getBlackList1(String filePath) throws IOException {
    FileInputStream fis = new FileInputStream(filePath);
    List<String> blackLists = new ArrayList<>();
    //Construct BufferedReader from InputStreamReader
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    String line = null;
    while ((line = br.readLine()) != null) {
      blackLists.add(line.trim());
    }
    br.close();
    fis.close();
    return blackLists;
  }

}
